//
//  Camera.cpp
//  RayTracer
//
//  Created by 俞云康 on 2/15/16.
//  Copyright © 2016 yushroom. All rights reserved.
//

#include "Camera.hpp"
#include "Params.hpp"

Camera::Camera(float3 position, float3 direction, float aspect, float fov)
: position(position), direction(Normalize(direction)) {
    //        if (fabsf(direction.y - 1) < EPS) { // direction is up
    //            cx = float3(aspect*fov, 0, 0);
    //            cy = float3(0, 0, -fov);
    //        } else {
    //            cy = float3(0, fov, 0);
    //            cx = Normalize(Cross(cy, direction)) * aspect * fov;
    //        }
    assert(fabsf(this->direction.y - 1) > EPS);
    cy = float3(0, 1, 0);
    cx = Normalize(Cross(cy, this->direction)) * (aspect * fov);
    //cx = float3(aspect*fov, 0, 0);
    cy = Normalize(Cross(this->direction, cx)) * fov;
    //cx = Normalize(Cross(cy, this->direction)) * (aspect * fov);
}

Camera Camera::create(const Params& params, float aspect) {
    auto look_at_params = params.child("lookAt");
    Position eye = look_at_params.getFloat3("origin");
    Position at  = look_at_params.getFloat3("target");
    Position up  = look_at_params.getFloat3("up");
    float fov = params.getFloat("fov");
    Direction direction = Normalize(at - eye);
    return Camera(eye, direction, aspect, radians(fov));
}